﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using MovieDB.Common;
using MovieDB.Controllers.Common;
using MovieDB.DTOs;
using MovieDB.Models;
using MovieDB.ServiceInterfaces;

namespace MovieDB.Controllers
{
    public class HomeController : MovieBaseControler
    {
        private readonly int _amountOfRecentMovies;
        private readonly ICategoryService _categoryService;
        private readonly IMovieService _movieService;
        private readonly int _pageSize;
        private readonly ISearchService _searchService;


        public HomeController(IMovieService movieService, ICategoryService categoryService, ISearchService searchService)
        {
            _pageSize = int.Parse(ConfigurationManager.AppSettings["PageSize"]);
            _amountOfRecentMovies = int.Parse(ConfigurationManager.AppSettings["AmountOfRecentMovies"]);
            _movieService = movieService;
            _categoryService = categoryService;
            _searchService = searchService;
        }

        //
        // GET: /Home/
        public ActionResult Index(Guid? id = null, int page = 0)
        {
            var pagedMovieDto = new PagedMovieDto();
            if (id != null)
            {
                MovieDto movie = _movieService.GetMovieById(id.Value);
                pagedMovieDto.Movies.Add(movie);
                pagedMovieDto.TotalAmountOfMovies = 1;
                TempData["Autocomplete"] = "true";
            }
            else
            {
                pagedMovieDto = _movieService.GetAllMoviesPaged(page, _pageSize);
            }

            var movies = Mapping.MapFromSourceToDestination<IEnumerable<MovieDto>, IEnumerable<MovieModel>>(pagedMovieDto.Movies);
            var nextPageBaseUrl = Url.Action("Index", "Home");

            var pagedMovieModel = ModelFactory.CreatePagedMovieModel(movies, nextPageBaseUrl, page, _pageSize, pagedMovieDto.TotalAmountOfMovies);

            return View(pagedMovieModel);
        }

        [HttpGet]
        public ActionResult Category()
        {
            CategoryDto movieCategories = _categoryService.GetMovieCategories();
            var movieCategoryModel = Mapping.MapFromSourceToDestination<CategoryDto, CategoryModel>(movieCategories);

            return View(movieCategoryModel);
        }

        [HttpGet]
        public ActionResult SelectedCategory(string id, int page = 0)
        {
            MovieGenre movieGenre = id.ParseEnumSafe(MovieGenre.Error);
            VideoType videoType = id.ParseEnumSafe(VideoType.Error);

            PagedMovieDto pagedMovieDto = movieGenre != MovieGenre.Error
                                              ? _searchService.GetMovieByGenre(movieGenre, page, _pageSize)
                                              : _searchService.GetMovieByVideoType(videoType, page, _pageSize);

            var movies = Mapping.MapFromSourceToDestination<IEnumerable<MovieDto>, IEnumerable<MovieModel>>(pagedMovieDto.Movies);
            var nextPageBaseUrl = Url.Action("SelectedCategory", "Home");

            var pagedMovieModel = ModelFactory.CreatePagedMovieModel(movies, nextPageBaseUrl, page, _pageSize, pagedMovieDto.TotalAmountOfMovies);
            ViewBag.CategoryName = movieGenre != MovieGenre.Error
                                       ? movieGenre.GetEnumDescription()
                                       : videoType.GetEnumDescription();

            return View(pagedMovieModel);
        }

        [HttpGet]
        public ActionResult MostRecentMovies()
        {
            IEnumerable<MovieDto> recentMovieDtos = _movieService.GetMostRecentMovies(_amountOfRecentMovies);
            var recentMovieModels = Mapping.MapFromSourceToDestination<IEnumerable<MovieDto>, IEnumerable<MovieModel>>(recentMovieDtos);

            ViewBag.AmountOfRecentMovies = _amountOfRecentMovies;
            return View(recentMovieModels);
        }
    }
}